﻿using System;

// 定义形状接口
public interface IShape
{
    string Name { get; } // 形状名称
    double CalculateArea();
    bool IsValid();
}

// 抽象类 Shape，实现 IShape 接口
public abstract class Shape : IShape
{
    public abstract string Name { get; } // 抽象属性，子类必须实现
    public abstract double CalculateArea();
    public abstract bool IsValid();
}

// 长方形类
public class Rectangle : Shape
{
    public override string Name => "Rectangle"; // 实现 Name 属性
    public double Width { get; set; }
    public double Height { get; set; }

    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }

    public override double CalculateArea()
    {
        return Width * Height;
    }

    public override bool IsValid()
    {
        return Width > 0 && Height > 0;
    }
}

// 正方形类
public class Square : Shape
{
    public override string Name => "Square"; // 实现 Name 属性
    public double Side { get; set; }

    public Square(double side)
    {
        Side = side;
    }

    public override double CalculateArea()
    {
        return Side * Side;
    }

    public override bool IsValid()
    {
        return Side > 0;
    }
}

// 圆形类
public class Circle : Shape
{
    public override string Name => "Circle"; // 实现 Name 属性
    public double Radius { get; set; }

    public Circle(double radius)
    {
        Radius = radius;
    }

    public override double CalculateArea()
    {
        return Math.PI * Radius * Radius;
    }

    public override bool IsValid()
    {
        return Radius > 0;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Random random = new Random();
        Shape[] shapes = new Shape[10];
        double totalArea = 0;

        for (int i = 0; i < shapes.Length; i++)
        {
            int shapeType = random.Next(0, 3); // 随机生成形状类型

            switch (shapeType)
            {
                case 0:
                    shapes[i] = new Rectangle(random.NextDouble() * 10, random.NextDouble() * 10);
                    break;
                case 1:
                    shapes[i] = new Square(random.NextDouble() * 10);
                    break;
                case 2:
                    shapes[i] = new Circle(random.NextDouble() * 10);
                    break;
            }

            if (shapes[i].IsValid())
            {
                double area = shapes[i].CalculateArea();
                Console.WriteLine($"{shapes[i].Name} Area: {area:F2}"); // 输出形状名称和面积
                totalArea += area;
            }
            else
            {
                Console.WriteLine($"Invalid {shapes[i].Name} encountered and skipped.");
            }
        }

        Console.WriteLine($"\nTotal area of all valid shapes: {totalArea:F2}");
    }
}